←Select platform

ResizeBuffer(int,int,byte[],int,int) Method

Summary
Retrieves resized data, one line at a time.
Syntax
C#
C++/CLI
Python
public void ResizeBuffer( 
   int row, 
   int column, 
   byte[] buffer, 
   int bufferOffset, 
   int bytes 
) 
public: 
void ResizeBuffer(  
   int row, 
   int column, 
   array<byte>^ buffer, 
   int bufferOffset, 
   int bytes 
)  
def ResizeBuffer(self,row,column,buffer,bufferOffset,bytes): 

Parameters

row
The number of the row to retrieve. The first row is 0, and the last row is 1 less than the image Height.

column
The column offset within the row to retrieve. The first column offset is 0, and the last column offset is 1 less than the image Width.

buffer
The buffer to hold the image data that this method gets.

bufferOffset
The zero-based index into  buffer where retrieving should start.

bytes
The number of bytes to retrieve. Consider the BitsPerPixel, and avoid specifying a number that goes past the end of the row.

  • For a 1-bit image, each byte represents 8 pixels.
  • For a 4-bit image, each byte represents 2 pixels.
  • For an 8-bit image, each byte represents 1 pixel.
  • For a 16-bit image, every 2 bytes represents one pixel.
  • For a 24-bit image, every three bytes represents one pixel.
  • For a 32-bit image, every four bytes represents one pixel.
  • For a 48-bit image, every six bytes represents one pixel.
  • For a 64-bit image, every eight bytes represents one pixel.

You can use the BitsPerPixel property with integer math to calculate the number of bytes needed for a particular number of pixels. For example:

NumberOfBytes = Image.BitsPerPixel * (Image.Width * Image.Height + 7) / 8;
Remarks

By using this low-level method to get any part of a row, you can write a procedure that accesses a single pixel or a rectangular area within the image.

This method accepts an offset parameter ( column) in pixels and a length ( bytes) in bytes. Therefore, you must consider the BitsPerPixel of the image when specifying these parameters. The following table describes the rules:

BitsPerPixel Column Offset (in Pixels) Bytes to Get
1 Must be a multiple of 8 (such as 0,8, or 16). Can be any number up to the end of the row. Consider that there are 8 pixels per byte.
4 Must be an even number (such as 0, 2, or 4). Can be any number up to the end of the row. Consider that there are 2 pixels per byte.
8 Can be any column within the image. Can be any number up to the end of the row. Consider that there is 1 pixel per byte.
16 Can be any column within the image. Must be a multiple of 2 (such as 2, 4, or 6), because there are 2 bytes per pixel.
24 Can be any column within the image. Must be a multiple of 3 (such as 3, 6, or 9), because there are 3 bytes per pixel.
32 Can be any column within the image. Must be a multiple of 4 (such as 4, 8, or 12), because there are 4 bytes per pixel.
48 Can be any column within the image. Must be a multiple of 6 (such as 6, 12, or 18), because there are 6 bytes per pixel.
64 Can be any column within the image. Must be a multiple of 8 (such as 8, 16, or 24), because there are 8 bytes per pixel.

Do not pass a value in  bytes that goes past the end of the row. The process is faster when rows are retrieved sequentially, either top-down or bottom-up.

For more information, refer to Introduction to Image Processing With LEADTOOLS.

Example
C#
using Leadtools; 
using Leadtools.Codecs; 
 
 
public void RasterImageResizeExample() 
{ 
	string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
	string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_RasterImageResize.bmp"); 
 
	using (RasterCodecs codecs = new RasterCodecs()) 
	{ 
		// Load the source image 
		using (RasterImage srcImage = codecs.Load(srcFileName)) 
		{ 
			// We will resize to half the original size 
			int destWidth = srcImage.Width / 2; 
			int destHeight = srcImage.Height / 2; 
 
			// Create the destination image 
			using (RasterImage destImage = new RasterImage( 
			   RasterMemoryFlags.Conventional, 
			   destWidth, 
			   destHeight, 
			   srcImage.BitsPerPixel, 
			   srcImage.Order, 
			   srcImage.ViewPerspective, 
			   srcImage.GetPalette(), 
			   IntPtr.Zero, 
			   0)) 
			{ 
				RasterImageResize resize = new RasterImageResize(); 
 
				// Add Event Handler 
				resize.Resize += new EventHandler<RasterImageResizeEventArgs>(resize_Resize); 
 
				byte[] buffer = new byte[destImage.BytesPerLine]; 
 
				// Start the resize process 
				resize.Start( 
				   srcImage, 
				   destWidth, 
				   destHeight, 
				   srcImage.BitsPerPixel, 
				   srcImage.Order, 
				   srcImage.DitheringMethod, 
				   RasterSizeFlags.None, 
				   srcImage.GetPalette()); 
 
				destImage.Access(); 
				// get the rows for the resized image, one by one 
				for (int row = 0; row < destImage.Height; row++) 
				{ 
					resize.ResizeBuffer(row, 0, buffer, 0, destImage.BytesPerLine); 
					destImage.SetRow(row, buffer, 0, destImage.BytesPerLine); 
				} 
				destImage.Release(); 
				resize.Stop(); 
 
				// Save the destination image 
				codecs.Save(destImage, destFileName, RasterImageFormat.Bmp, 24); 
			} 
		} 
	} 
} 
 
private void resize_Resize(object sender, RasterImageResizeEventArgs e) 
{ 
	// e.Row should ALWAYS be less than e.Image.Height 
	if (e.Row >= e.Image.Height) 
	{ 
		e.Cancel = true; // abort the resize 
		return; 
	} 
 
	byte[] buffer = new byte[e.Bytes]; 
	e.Image.Access(); 
	e.Image.GetRowColumn(e.Row, e.Column, buffer, 0, e.Bytes); 
	e.Image.Release(); 
	Marshal.Copy(buffer, 0, e.Buffer, e.Bytes); 
	Console.WriteLine("{0}, {1}", e.Row, e.Column); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

Help Version 22.0.2023.5.16
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.